﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using My.Common.AutoMapper;
using My.Common.Configuration;
using My.Common.EntityHelper;
using My.Common.Error;
using My.Pattern.Builder;
using My.Web.MVC.V4.Factory;
using NCLCustomerManagement.BusinessLogic.History;
using NCLCustomerManagement.Common.Constant;
using NCLCustomerManagement.DataTransferObjects;
using NCLCustomerManagement.DataTransferObjects.History;
using NCLCustomerManagement.UI.Helper;

namespace NCLCustomerManagement.UI.Controllers
{
    public class BaseController : Controller
    {
        //public ISaveHandler SaveHandler { get; set; }
        //public IDeleteHandler DeleteHandler { get; set; }
        public IEntityHelper DTOHelper { get; set; }
        public IAutoMapperBuilder AutoMapperBuilder { get; set; }
        public IErrorLog ErrorLog { get; set; }
        public IPartialViewRender PartialViewRender { get; set; }
        public JavaScriptSerializer JsSerializer { get; set; }
        public IErrorBuilder ErrorBuilder { get; set; }

        public IHistoryBC HistoryBC { get; set; }
        #region Public Methods

        //protected UserDTO GetCurrentUser()
        //{
        //    return SessionManager.SessionCache.GetUserContextData();
        //}

        //protected string GetLogedInUser()
        //{
        //    return SessionManager.SessionCache.GetLogedInUser();
        //}

        public void AddToHistory(string message, HistoryType type = HistoryType.Other)
        {
            HistoryBC.AddHistory(new DataTransferObjects.History.HistoryDTO()
            {
                Id = Guid.NewGuid(),
                Operation = message,
                OperationDate = DateTime.Now,
                Type = type,
                Username = this.GetCurrentUser().Username
            });
        }
        #endregion





        #region Virtual Mapping method

        protected virtual DTO ConvertModelToDTO<Model, DTO>(Model model)
            where Model : class
            where DTO : class, new()
        {
            return AutoMapperBuilder.MapSourceToDestination<Model, DTO>(model);
        }

        internal virtual Model ConvertDTOToModel<DTO, Model>(DTO dto)
            where DTO : class
            where Model : class, new()
        {
            return AutoMapperBuilder.MapSourceToDestination<DTO, Model>(dto);
        }

        #endregion
    }
}